import json

from flask import Blueprint, jsonify

from libs.db import DB

account_bp = Blueprint('account', __name__)

@account_bp.route('/accounts', methods=['GET'])
def list_accounts():
    sql = """
    SELECT user_id, user_no, user_name, is_vip, vip_expire_date, fee_rate, rep, config, 
    status, remark, email, twitter, discord 
    FROM account
    """
    rows = DB().fetch_all(sql)
    result = {}
    for row in rows:
        result[str(row[0])] = {
            "user_no": row[1],
            "user_name": row[2],
            "is_vip": row[3],
            "vip_expire_date": row[4],
            "fee_rate": row[5],
            "rep": row[6],
            "config": json.loads(row[7]),
            "status": row[8],
            "remark": row[9],
            "email": row[10],
            "twitter": row[11],
            "discord": row[12],
        }

    return jsonify({"code": 0, "list": result})
